package com.qfjy.service.impl;

import com.qfjy.common.utils.exception.MeetingGrabException;
import com.qfjy.entity.po.MeetingGrab;
import com.qfjy.mapper.MeetingGrabMapper;
import com.qfjy.service.MeetingGrabService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName MeetingGrabServiceImpl
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/12/8
 * @Version 1.0
 */
@Service
@Slf4j
public class MeetingGrabServiceImpl implements MeetingGrabService {

    @Autowired
    private MeetingGrabMapper meetingGrabMapper;
    /**
     * 会议进行抢单
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(MeetingGrab meetingGrab) {
        meetingGrab.setId(UUID.randomUUID().toString());
        meetingGrab.setGrabStatus((short) 0);
        meetingGrab.setStatus((short) 1);
        meetingGrab.setCreateDate(new Date());
        return meetingGrabMapper.insertSelective(meetingGrab);
    }

    /**
     * TODO 根据发单ID 获取抢单者列表
     *
     * @param pid 发单ID
     * @return
     */
    @Override
    public List<MeetingGrab> selectGrabListByPid(String pid) {
        return meetingGrabMapper.selectGrabListByPid(pid);
    }

    /**
     * TODO 就选你功能
     * 1、将pid全部的抢单者状态改为2
     * 2、将pid 指定的uid状态改为1
     *
     * @param pid
     * @param uid
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)  //什么时候会回滚？ 抛出运行时异常。RunTimeException
    public int chooseGrabByUidAndPid(String pid, int uid)  throws MeetingGrabException{

        //1、将pid全部的抢单者状态改为2

        int num=meetingGrabMapper.updateMeetingGrabMatchFail(pid);
        if(num<1){
            log.error("更新失败{}",num);
            throw  new MeetingGrabException("抢单状态更改失败");
        }

        //2、将pid 指定的uid状态改为1
        int num1= meetingGrabMapper.updateMeetingGrabMatchSucc(pid,uid);
        if(num1<1){
            throw  new MeetingGrabException("抢单状态匹配成功者更改失败");
        }
        return 1;
    }
}
